/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Ejercicio2;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
/**
 *
 * @author Usuario
 */
public class MRU extends javax.swing.JFrame {

    //atributos
    private DefaultListModel Disco;
    //contador : del tamaño del disco duro
    private int i;
    //contador : de la memoria RAM
    private int j;
    //variable menos usada
    private int posicionR;
    //posicion de la variable del disco duro
    private int posicionD;
    //variable de tipo model de RAM
    private DefaultListModel RAM;
    
    public MRU() {
        initComponents();
        //inicializamos la variable de tipo Listmodel
        this.Disco =   new DefaultListModel();
        //agregamos el modelo a ala lista
        //ListaDatos.setModel(modelo);
        //iniciando la variable contadora
        this.i=0;
        this.j=0;
        //posicion de la variable ram menos usada
        this.posicionR=0;
        //posicion de la variable del disco duro
        this.posicionD=0;
        //inicializamos la variable de tipo Listmodel RAM
        this.RAM= new DefaultListModel();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtDisco = new javax.swing.JTextField();
        txtRAM = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txtElemento = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        ListaDatos = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        buAgregar = new javax.swing.JButton();
        buSimular = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        ListaMRU = new javax.swing.JList();
        jLabel5 = new javax.swing.JLabel();
        buLimpiar = new javax.swing.JButton();
        buSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 204));
        jLabel1.setText("Simulacion del MRU");

        jLabel3.setForeground(new java.awt.Color(0, 0, 204));
        jLabel3.setText("Tamaño de Disco :");

        jLabel4.setForeground(new java.awt.Color(0, 0, 204));
        jLabel4.setText("Tamaño de RAM :");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel6.setForeground(new java.awt.Color(0, 0, 204));
        jLabel6.setText("Elemento:");

        jScrollPane1.setViewportView(ListaDatos);

        jLabel2.setForeground(new java.awt.Color(0, 0, 204));
        jLabel2.setText("Disco Duro :");

        buAgregar.setText("Agregar");
        buAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buAgregarActionPerformed(evt);
            }
        });

        buSimular.setText("Simular");
        buSimular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buSimularActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(ListaMRU);

        jLabel5.setForeground(new java.awt.Color(0, 0, 204));
        jLabel5.setText("Memoria Principal :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(166, 166, 166)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buAgregar)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
                        .addComponent(txtElemento, javax.swing.GroupLayout.Alignment.LEADING)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(buSimular)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(53, 53, 53))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(95, 95, 95)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtElemento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(buAgregar))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(59, 59, 59)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(81, 81, 81)
                                        .addComponent(buSimular))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addGap(18, 18, 18)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(0, 80, Short.MAX_VALUE)))
                .addContainerGap())
        );

        buLimpiar.setText("Limpiar");
        buLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buLimpiarActionPerformed(evt);
            }
        });

        buSalir.setText("Salir");
        buSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(169, 169, 169)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtDisco, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                            .addComponent(txtRAM))
                        .addGap(51, 51, 51)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(buLimpiar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(buSalir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(28, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtDisco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtRAM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(buSalir)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(buLimpiar)))
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buAgregarActionPerformed
        // TODO add your handling code here:
        
        int tamDisco= Integer.parseInt(txtDisco.getText());
        //si i es menor que el tamaño de disco
        if(i<tamDisco)
        {
            Disco.addElement(txtElemento.getText());
            //agregamos a la lista el objeto ListModel
            ListaDatos.setModel(Disco);
            //limpiamos el txtElemento
            txtElemento.setText("");
            //actualizamos el contador
            i++;
            //txtElemento.focus();
            txtElemento.requestFocus();
        }
        else
        {
            JOptionPane.showMessageDialog(null,"Disco Duro lleno");
        }
    }//GEN-LAST:event_buAgregarActionPerformed

    private void buSimularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buSimularActionPerformed
        // TODO add your handling code here:
        //determinamos si la memoria esta lleno
        //JOptionPane.showMessageDialog(null,modelo.getElementAt(2).toString());
        int tamRam= Integer.parseInt(txtRAM.getText());
        int tamDisco= Integer.parseInt(txtDisco.getText());
        posicionR=tamRam-1;
        if(j<tamRam)
        {
            RAM.addElement(Disco.getElementAt(j));
            ListaMRU.setModel(RAM);
            posicionD++;
            j++;
        }
        else
        {
            if(posicionD<tamDisco)
            {
                //remplazamos la posicion menos usada de disco dura por la ram
                //JOptionPane.showMessageDialog(null,"Memoria RAM LLena");
                RAM.setElementAt(Disco.getElementAt(posicionD),posicionR);
                posicionD++;
                /*if(posicionR+1<tamRam)
                {
                    posicionR++;
                }
                else
                {
                    posicionR=0;
                }*/
                //ListaRLU.setModel(RAM);
            }
            else
            JOptionPane.showMessageDialog(null,"NO hay mas elementos");
        }
    }//GEN-LAST:event_buSimularActionPerformed

    private void buLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buLimpiarActionPerformed
        // TODO add your handling code here:
        txtDisco.setText("");
        txtElemento.setText("");
        txtRAM.setText("");
        this.Disco =   new DefaultListModel();
        //agregamos el modelo a ala lista
        //ListaDatos.setModel(modelo);
        //iniciando la variable contadora
        this.i=0;
        this.j=0;
        //posicion de la variable ram menos usada
        this.posicionR=0;
        //posicion de la variable del disco duro
        this.posicionD=0;
        //inicializamos la variable de tipo Listmodel RAM
        this.RAM= new DefaultListModel();
        //limpiar las listas
        ListaDatos.setModel(Disco);
        ListaMRU.setModel(RAM);
    }//GEN-LAST:event_buLimpiarActionPerformed

    private void buSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buSalirActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_buSalirActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MRU.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MRU.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MRU.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MRU.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MRU().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList ListaDatos;
    private javax.swing.JList ListaMRU;
    private javax.swing.JButton buAgregar;
    private javax.swing.JButton buLimpiar;
    private javax.swing.JButton buSalir;
    private javax.swing.JButton buSimular;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField txtDisco;
    private javax.swing.JTextField txtElemento;
    private javax.swing.JTextField txtRAM;
    // End of variables declaration//GEN-END:variables
}
